package com.arch.bigdata.hbase1x.lx;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author pizhihui
 * @date 2020-10-10
 */
public class LxRealTimeHbaseClient {

    private static final Logger LOG = LoggerFactory.getLogger(LxRealTimeHbaseClient.class);


    private final Configuration hbaseConf;
    private volatile Connection connection;

    public LxRealTimeHbaseClient(String bid, String clusterName) {
        this(bid, clusterName, "USE_DEFAULT_CONF");
        this.initHbaseConnection();
    }

    public LxRealTimeHbaseClient(String bid, String clusterName, String specificConf) {
        this.hbaseConf = HBaseConfiguration.create();
        this.connection = null;
        System.out.println("构造函数");
//        this.bussName = "unknown";
//        this.lockForLocator = new ReentrantLock();
//        this.tableNameToRefreshTimeMap = new HashMap();
//        this.tableNameHRegionLocatorMap = new HashMap();
//        this.bussName = "LX_HBC_" + bid;
//        LOG.warn("LxRealTimeHbaseClient|version|V01_2019.07.17_001|bussName|" + this.bussName + "|" + specificConf + "|clusterName|" + clusterName);
//        String clusterAddress = this.getClusterAddFromName(clusterName);
        String clusterAddress = null;
        this.addHbaseConfig("hbase.zookeeper.quorum", clusterAddress);
    }

    public int addHbaseConfig(String confKey, String confValue) {
        if (confKey != null && confValue != null) {
            this.hbaseConf.set(confKey, confValue);
            return 0;
        } else {
            LOG.error("addHbaseConfig|confKey == null || confValue == null");
            return -100;
        }
    }

    public int initHbaseConnection() {
        this.addHbaseConfig("hadoop.job.ugi", "biadmin");
        this.addHbaseConfig("hbase.client.userprovider", "biadmin");
        if (this.hbaseConf.get("hbase.zookeeper.quorum") == null) {
            throw new RuntimeException("initHbaseConnection|hbase client connetion is null, please init client before use!");
        } else {
            return this.reinitHbaseConnectionWithoutLock();
        }
    }

    private int reinitHbaseConnectionWithoutLock() {
        try {
            this.connection = ConnectionFactory.createConnection(this.hbaseConf);
            return 0;
        } catch (Exception var2) {
            LOG.error("initHbaseConnection|", var2);
            throw new RuntimeException("reinitHbaseConnectionWithoutLock|hbase client connetion init error!|", var2);
        }
    }

}
